// var list = require('../../utils/list.js')

import { getCheckList, submitReportItems } from "../../utils/networkRequest.js"
import { errorMsg } from '../../utils/tool.js'

//获取应用实例
const app = getApp()
Page({
  data: {
    // 左侧点击类样式
    curNav: '',
    list: [],
    submitArr: [],//要提交的检测项
    qcode: null,
  },
  onLoad: function (options) {
    this.setData({
      qcode: options.qc,
      report_id: options.report_id
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    // var listChild1 = list.List[0];
    // wx.navigateToMiniProgram({
    //   appId: 'wx0da2defc20ec24b3',
    //   path: 'pages/index/index?id=123',
    //   extraData: {
    //     foo: 'bar'
    //   },
    //   envVersion: 'develop',
    //   success(res) {
    //     // 打开成功
    //   }
    // })
//report_id
    getCheckList(this.data.report_id,(r) => {
      if (r.code == 0) {
        this.setData({
          list: r.data,
          curNav: r.data[0].target,
          colorList: ["#4b0", "#f85", "red"],
        })

        //将数据放入全局变量中
        app.globalData.list = r.data;


      } else {
        errorMsg(r.message);
      }

    })


    var that = this;
    // 获取可视区高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight,
        })
      }
    })
  },

  onShow: function () {
    this.setData({
      list: app.globalData.list
    })
  },
  //点击左侧 tab ，右侧列表相应位置联动 置顶
  switchRightTab: function (e) {
    var id = e.target.id;
    console.log(typeof id)
    this.setData({
      // 动态把获取到的 id 传给 scrollTopId
      scrollTopId: id,
      // 左侧点击类样式
      curNav: id
    })
  },
  //选择检测项
  onCheckItem: function (r) {
    if (r.currentTarget.dataset.item) {
      wx.navigateTo({
        url: '../changeiteminfo/changeiteminfo?item=' + JSON.stringify(r.currentTarget.dataset.item)
      })
    }

  },
  //批量设置良好
  totalCheck: function () {
    let { list } = this.data;
    list.map((o, i) => {
      if (o.children) {
        o.children.map((oo, ii) => {
          if (oo.children) {
            oo.children.map((ooo, iii) => {
              if (ooo.children) {
                ooo.children.map((oooo, iiii) => {
                  //将选中的值放入 数组
                  if (oooo.isSelect) {
                    ooo.isSelect = true;
                    oo.isSelect = true;
                    o.isSelect = true;
                  }
                })
                if (!ooo.isSelect) {
                  //将没选中的选项全部改为选中良好状态
                  ooo.children.map((oooo, iiii) => {
                    ooo.jibie = 1; //选择良好
                    if (oooo.level == 1) {
                      oooo.isSelect = true;
                      ooo.selectItem = oooo.title;//良好标题
                    }
                    ooo.isSelect = true;
                    oo.isSelect = true;
                    o.isSelect = true;
                  })
                }
              }
            })
          }
        })
      }

    })
    this.setData({
      list: this.data.list
    })
    app.globalData.list = this.data.list;
  },
  //提交
  checkItemSubmit: function () {

    let { list, report_id, qcode } = this.data;
    let self = this;
    let tempArr = [];
    list.map((o, i) => {
      o.children.map((oo, ii) => {
        if (oo.children) {
          oo.children.map((ooo, iii) => {
            if (ooo.children) {
              ooo.children.map((oooo, iiii) => {
                if (oooo.isSelect) {
                  tempArr.push({ checklist_id: oooo.id, color_grade: oooo.level, parent_id: oooo.parent_id });
                }
              })
              self.setData({
                submitArr: tempArr
              })
            }
          })
        }
      })
    })
    if (this.data.submitArr.length > 0) {
      submitReportItems(report_id, qcode, this.data.submitArr, (r) => {
        if (r.code == 0) {
          wx.navigateTo({
            url: '../preview/preview?report_id=' + report_id + '&qcode=' + qcode
          })
        }
      })
    } else {
      errorMsg("请录入检测结果")
    }

  }

})
